import { updateInfoApi } from "@/api/client/documentManagement/advanceManagement";
import { chooseAddressPageApi } from "@/api/waybillManagement/stockManagement";
import { message } from "ant-design-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { tableData, loading, emit, tableRef, selectRow, passData } = state;
  const init = () => {
    loading.value = true;
    return chooseAddressPageApi({ customerId: passData.value.customerId, status: 1, pageNum: 1, pageSize: 999999 } as ClientAddressManagement.SearchForm)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          const row = tableData.value.find((t: any) => t.id === passData.value.pickupAddressId);
          tableRef.value.setRadioRow(row);
          selectRow.value = row;
        }
      })
      .finally(() => (loading.value = false));
  };
  const clearRadioRowEevnt = () => {
    tableRef.value.clearRadioRow();
  };
  const radioChangeEvent = ({ row }: any) => {
    selectRow.value = row;
  };
  const submitHandle = () => {
    if (!selectRow.value) return message.error("请选择地址!");
    loading.value = true;
    updateInfoApi({
      id: passData.value.preOrderId,
      pickupAddressId: selectRow.value.id
    })
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };
  const cancelHandle = () => {
    emit("update:visible", false);
  };
  return {
    init,
    submitHandle,
    cancelHandle,
    clearRadioRowEevnt,
    radioChangeEvent
  };
}
